package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.MyShopOrderListResponse
import com.gexiaobao.pigeon.app.util.SettingUtil
import com.gexiaobao.pigeon.databinding.ItemAllShopOrderBinding
import java.math.BigDecimal

/**
 * @author: hxw
 * @date  : 2023/2/2
 * @desc  :
 */
class AllShopOrderListAdapter(data: ArrayList<MyShopOrderListResponse.ListBean>) :
    BaseQuickAdapter<MyShopOrderListResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_all_shop_order, data) {

    private var mOnItemClickListener: OnChildItemClickListener? = null

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: MyShopOrderListResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemAllShopOrderBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        if (!item.isEmpty()) {
            holder.setText(R.id.tvItemGoodsCount, "共" + item.list.size.toString() + "件")
        } else {
            holder.setText(R.id.tvItemGoodsCount, "共0件")
        }

        when (item.orderStatus) {
            2 -> {//支付成功
                holder.setGone(R.id.tvItemButton1, true)
//                if (!item.isEmpty() && item.list.size != 1) {
                holder.setGone(R.id.tvItemButton2, true)//申请退款
//                } else {
//                    holder.setGone(R.id.tvItemButton2, false)
//                }
                holder.setGone(R.id.tvItemButton3, false)//确认收货
                holder.setGone(R.id.tvItemButton4, false)//再来一单
                holder.setGone(R.id.tvItemButton5, false)//延长收货
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Delete_order))//"删除订单"
                holder.setText(R.id.tvItemButton2, context.getString(R.string.application_for_drawback))//"申请退款"
                holder.setText(R.id.tvItemButton3, context.getString(R.string.activity_market_shop_order_payfor_delete_modify))//修改订单
            }

            3 -> {//已发货
                holder.setGone(R.id.tvItemButton1, false)
//                if (!item.isEmpty() && item.list.size != 1) {
                holder.setGone(R.id.tvItemButton2, true)//申请退款
//                } else {
//                    holder.setGone(R.id.tvItemButton2, false)
//                }
                holder.setGone(R.id.tvItemButton3, false)//显示
                holder.setGone(R.id.tvItemButton4, false)//再来一单
                holder.setGone(R.id.tvItemButton5, false)//延长收货
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Logistics_schedule))//物流进度
                holder.setText(R.id.tvItemButton2, context.getString(R.string.a_return_request))//申请退货
                holder.setText(R.id.tvItemButton3, context.getString(R.string.confirm_receipt))//确认收货
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.buy_now_btn_red)
                holder.setTextColor(R.id.tvItemButton3, ContextCompat.getColor(context, R.color.white))
            }

            4 -> {//已签收
                holder.setGone(R.id.tvItemButton1, false)
                if (!item.isEmpty() && item.list.size != 1) {
                    holder.setGone(R.id.tvItemButton2, true)//申请退款
                } else {
                    holder.setGone(R.id.tvItemButton2, false)
                }
                holder.setGone(R.id.tvItemButton3, true)//隐藏
                holder.setGone(R.id.tvItemButton4, false)//（显示）再来一单
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Delete_order))//删除订单
                holder.setText(R.id.tvItemButton2, context.getString(R.string.Return_after_sales))//"退货/售后"
                holder.setText(R.id.tvItemButton3, context.getString(R.string.buy_again))//"再次购买"
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            5 -> {
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, false)//隐藏
                holder.setText(R.id.tvItemButton3, context.getString(R.string.pay_again))//重新支付
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            7 -> {//退款成功
                holder.setGone(R.id.tvItemButton1, false)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, false)//隐藏
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Delete_order))//删除订单
                holder.setText(R.id.tvItemButton3, context.getString(R.string.buy_again))//再次购买
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            9 -> {//订单关闭
                holder.setGone(R.id.tvItemButton1, false)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, true)//显示
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Delete_order))//删除订单
                holder.setText(R.id.tvItemButton3, context.getString(R.string.buy_again))//再次购买
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            10 -> {//已评价
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, true)//显示
                holder.setText(R.id.tvItemButton3, context.getString(R.string.buy_again))//再次购买
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            11 -> {//退货中
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, false)//隐藏
                holder.setText(R.id.tvItemButton3, context.getString(R.string.Return_details))//退货详情
            }

            12 -> {//退货成功
                holder.setGone(R.id.tvItemButton1, false)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, true)
                holder.setText(R.id.tvItemButton1, context.getString(R.string.Delete_order))//删除订单
                holder.setText(R.id.tvItemButton3, context.getString(R.string.buy_again))//再次购买
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            13, 14 -> {
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, false)
                holder.setText(R.id.tvItemButton3, context.getString(R.string.Return_details))//退货详情
            }

            15 -> {
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, false)
                holder.setText(R.id.tvItemButton3, context.getString(R.string.out_information))//我已寄出
                holder.setBackgroundResource(R.id.tvItemButton3, R.drawable.border_red_to_pay)
                holder.setTextColor(R.id.tvItemButton3, Color.parseColor("#FF4500"))
            }

            else -> {
                holder.setGone(R.id.tvItemButton1, true)
                holder.setGone(R.id.tvItemButton2, true)
                holder.setGone(R.id.tvItemButton3, true)
            }
        }

        holder.itemView.apply {
            binding.itemRvMyShopOrder.adapter = null
            if (item.isEmpty()) {
                holder.setGone(R.id.itemRvMyShopOrder, true)
                return
            }
            holder.setGone(R.id.itemRvMyShopOrder, false)
            val mAdapterGoods = MyGoodsItemAdapter(item.list)
            binding.itemRvMyShopOrder.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapterGoods
                mAdapterGoods.notifyDataSetChanged()
            }

            mAdapterGoods.setOnItemClickListener { _, _, _ ->
                mOnItemClickListener?.onChildItemClick(item.orderId, item.orderStatus)
            }
        }

        val tvItemPriceTotal = holder.getView<AppCompatTextView>(R.id.tvItemPriceTotal)
        if (!item.isEmpty()) {
            var totalPrice = BigDecimal("0.00")
            item.list.forEachIndexed { _, listType ->
                val price = BigDecimal(listType.salePrice.toString())
                val count = BigDecimal(listType.amount.toString())
                val result = price.multiply(count)
                totalPrice = totalPrice.add(result)
            }
            SettingUtil.setPrice(totalPrice.toString(), tvItemPriceTotal, 50, Color.BLACK)
        }
    }

    fun setOnChildGoodsClickListener(listener: OnChildItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnChildItemClickListener {
        fun onChildItemClick(orderId: Int, orderStatus: Int)
    }
}